import { beforeCreate, belongsTo, column, scope } from '@adonisjs/lucid/orm'
import Dict from '#models/dict'
import type { BelongsTo } from '@adonisjs/lucid/types/relations'
import CommonBaseModel from '#common/base_model'
import type { ModelAttributes } from '@adonisjs/lucid/types/model'

export default class Member extends CommonBaseModel {
  static staff = scope((query) => {
    query.where('type', 2)
  })

  @column()
  declare fkSourceId: number | null

  @column()
  declare fkPostId: number | null

  @column()
  declare memberId: string

  @beforeCreate()
  static createMemberId(_member: ModelAttributes<Member>) {
    // member.memberId = nanoid()
  }

  @column()
  declare type: number

  @column()
  declare name: string

  @column()
  declare nickName: string

  @column()
  declare keywords: string

  @column()
  declare avatarUrl: string

  @column()
  declare sex: number

  @column()
  declare age: number

  @column()
  declare birthdayType: number

  @column()
  declare birthday: Date

  @column()
  declare lunarBirthday: Date

  @column()
  declare phone: string

  @column()
  declare email: string

  @column()
  declare openid: string

  @column()
  declare unionid: string

  @column({ serializeAs: null })
  declare password: string

  @column()
  declare status: number

  @column()
  declare memo: string

  @belongsTo(() => Dict)
  declare source: BelongsTo<typeof Dict>

  @belongsTo(() => Dict)
  declare post: BelongsTo<typeof Dict>
}
